package com.lz.controller;

import com.lz.domain.User;
import com.lz.redis.RedisService;
import com.lz.result.CodeMsg;
import com.lz.result.Result;
import com.lz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/demo")
public class SampleController {

    @Autowired
    UserService userService;

    @Autowired
    RedisService redisService;

    @RequestMapping("/")
    @ResponseBody
    String home(){
        return "hahn";
    }


    @RequestMapping("/hello")
    @ResponseBody
    public Result<String> hello(){
        return Result.success("hi,lizan");
        //return new Result(0,"success","hi,lz");
    }

    @RequestMapping("/helloError")
    @ResponseBody
    public Result<String> helloError(){
         return Result.error(CodeMsg.SERVER_ERROR);
    }

    @RequestMapping("/test")
    public String test(Model model){
        model.addAttribute("name","lizan");
        return "hello";
    }

    @RequestMapping("/db/get")
    @ResponseBody
    public Result<User> dbGet(){
        User user = userService.getById(1);
        return Result.success(user);
    }

    @RequestMapping("/db/tx")
    @ResponseBody
    public Result<Boolean> dbTx(){
        userService.tx();
        return Result.success(true);
    }

    @RequestMapping("/redis/set")
    @ResponseBody
    public Result<Long> redisGet(){
        Long v1 = redisService.get("key1",Long.class);
        return Result.success(v1);
    }

    @RequestMapping("/redis/get")
    @ResponseBody
    public Result<String> redisSet(){
        boolean ret = redisService.set("key2","hi,lz");
        String str = redisService.get("key2",String.class);
        return Result.success(str);
    }
}